Imports System.Windows.Forms
Imports RIBS.Code.GlobalModule1

Public Class frmFilter
    Dim Password As String = ""

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        If MsgBox("Save Settings?", MsgBoxStyle.YesNo, "Save Changes") = MsgBoxResult.Yes Then
            SaveAllSettings()
        End If
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Sub SaveAllSettings()
        Try
            Dim lStates As String = ""
            Dim lMainStatus As String = ""
            Dim lTaskStatus As String = ""
            Dim dgDR As DataGridViewRow
            Dim dt As DataTable = dgLetterCodes.DataSource
            '********** my Settings
            
            For Each dgDR In dgTaskState.Rows
                If dgDR.Cells(1).Value = True Then
                    lTaskStatus += dgDR.Cells(0).Value.ToString & ","
                End If
            Next

            For Each dgDR In dgMainState.Rows
                If dgDR.Cells(1).Value = True Then
                    lMainStatus += dgDR.Cells(0).Value.ToString & ","
                End If
            Next

            oNetworkUser.MainFilter = lMainStatus
            oNetworkUser.TaskFilter = lTaskStatus
            oNetworkUser.Save()
            '12/01
            'My.Settings.MyTaskStates = lTaskStatus
            'My.Settings.MyMainStates = lMainStatus
            'My.Settings.Save()
            'gsMyMainStates = lMainStatus
            'gsMyTaskStates = lTaskStatus
            'My.Settings.Save()

            Dim oRibs As New RibsClass
            oRibs.UpDateContactMethod(dgContactMenthod.DataSource)
            oRibs.UpDateStates(dgSystemStates.DataSource)
            oRibs.UpDateActions(dgsysRibsAction.DataSource)
            oRibs.UpdateRibsLetterCodes(dgLetterCodes.DataSource)
            oRibs.UpDateReason(dgReason.DataSource)

        Catch ex As Exception
            DisplayErrors(ex)
        End Try
    End Sub

    Private Sub frmFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim oRibs As New RibsClass
            Dim dt As DataTable = oRibs.GetRibsStates(True)
            Dim dtLetterCodes As DataTable = oRibs.GetRibsLetterCodes
            Dim dtActions As DataTable = oRibs.GetActions(True)
            Dim dtREason As DataTable = oRibs.GetReasons(True)
            Dim dr As DataRow
            Dim dsGetContactMethods As DataTable

            dsGetContactMethods = oRibs.GetContactMethods(True)
            dgContactMenthod.DataSource = dsGetContactMethods
            dgContactMenthod.Columns(0).Width = 150

            dgMainState.Rows.Clear()
            For Each dr In dt.Rows
                If dr("Closed") = False Then
                    dgMainState.Rows.Add()
                    dgMainState.Rows(dgMainState.Rows.Count - 1).Cells(0).Value = dr(0).ToString
                    If InStr(oNetworkUser.MainFilter, dr(0).ToString & ",") > 0 Then
                        dgMainState.Rows(dgMainState.Rows.Count - 1).Cells(1).Value = True
                    Else
                        dgMainState.Rows(dgMainState.Rows.Count - 1).Cells(1).Value = False
                    End If
                End If
            Next
            dgMainState.Columns(0).Width = 100
            dgMainState.Columns(0).ReadOnly = True

            dgTaskState.Rows.Clear()
            For Each dr In dt.Rows
                If dr("Closed") = False Then
                    dgTaskState.Rows.Add()
                    dgTaskState.Rows(dgTaskState.Rows.Count - 1).Cells(0).Value = dr(0).ToString
                    If InStr(oNetworkUser.TaskFilter, dr(0).ToString & ",") > 0 Then
                        dgTaskState.Rows(dgTaskState.Rows.Count - 1).Cells(1).Value = True
                    Else
                        dgTaskState.Rows(dgTaskState.Rows.Count - 1).Cells(1).Value = False
                    End If
                End If
            Next
            dgTaskState.Columns(0).Width = 100
            dgTaskState.Columns(0).ReadOnly = True

            dgSystemStates.DataSource = dt
            dgSystemStates.Columns("RibsState").HeaderText = "Status"

            '********* System Setting
            dgLetterCodes.DataSource = dtLetterCodes
            dgLetterCodes.Columns("Letter_Code").ReadOnly = True
            dgLetterCodes.Columns("Letter_Code").Width = 60
            dgLetterCodes.Columns("Letter_Code").HeaderText = "Letter Code"
            dgLetterCodes.Columns("Description").Width = 90

            dgsysRibsAction.Columns.Clear()
            dgsysRibsAction.DataSource = dtActions

            dgsysRibsAction.Columns("IsVisible").Visible = False
            dgsysRibsAction.Columns("Closed").Visible = True
            dgsysRibsAction.Columns("System").Visible = False

            dgsysRibsAction.Columns("Action").Width = 150
            dgsysRibsAction.AllowUserToAddRows = True
            dgsysRibsAction.Visible = True

            dgReason.DataSource = dtREason
            dgReason.Columns("Reason").Width = 150

            'chksysFuture.Checked = oSysSetting.HideFutureDated
            TabControl1.TabPages("User").Text = "User (" & CurrentUser & ")"
            dgContactMenthod.ReadOnly = True
        Catch ex As Exception
            DisplayErrors(ex)
        End Try

    End Sub


    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If TabControl1.SelectedIndex = 1 Then
            Dim oSec As New UserSecurity
            Dim cont As Control
            If oSec.CheckUserSecurity(NetWorkUserID, UserSecurity.RIBSSecurityAction.SystemSettings) Then
                For Each cont In TabControl1.TabPages(1).Controls
                    cont.Enabled = True
                Next
            Else
                For Each cont In TabControl1.TabPages(1).Controls
                    cont.Enabled = False
                Next
            End If
        End If
    End Sub
End Class
